<?php

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
 	require_once (APPLICATION_PATH . "\\models\\connectionDb.php");
} else {
    require_once (APPLICATION_PATH . "//models//connectionDb.php");
}

class Application_Model_DbTable_Status extends Zend_Db_Table_Abstract
{

    protected $_name = 'Status';
	protected $_primary = 'id';
	
	public function getStatus($id)
    {
     	$id = (int)$id;
  		$row = $this->fetchRow('id = ' . $id);         
  		if (!$row) {
   			return null;
  		}
  		return $row->toArray();
    }
    
	public function getStatuses($user_id)
    {
     	$user_id = (int)$user_id;
  		$rows = $this->fetchAll('User_id = ' . $user_id,"DateUpdate desc");
  					 
    	if (!$rows) {
   			return null;
  		}
  		return $rows->toArray();
    }
	public function getStatusesHome($user_id)
    {
     	$user_id = (int)$user_id;
     	$sql ="select Status.* ";
     	$sql .=" from Status";
     	//$sql .=" inner join friends on friends.Friend_id = ".$user_id;
		$sql .=" inner join Friends on Friends.Friend_id = Status.User_id"; //or Friends.User_id = Status.User_id";
		$sql .=" where 1=1";
		$sql .=" and Friends.User_id=".$user_id;
		$sql .=" order by DateUpdate desc";  
  		
		$db = connectionDb::connectDB();		
		$rows = $db->fetchAll($sql);
		
  					 
    	if (!$rows) {
   			return null;
  		}
  		return $rows;
    }

    
	    
	public function addStatus($user_id, $dateUpdate, $text, $mood)
 	{
  		$data = array(
	   		'User_id' => $user_id,
	   		'DateUpdate' => $dateUpdate,
	   		'Text' => $text,
  			'Mood' => $mood,	   			
  		);
  		$this->insert($data);  				
 	}	
 	
	public function deleteStatus($id)
	{	try{
			$this->delete('id =' . (int)$id);
		}
		catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
	}

}

